package com.pulaien.plug.controller;

import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.ProductUtil;
import com.pulaien.system.domain.dto.ExtGoodsParam;
import com.pulaien.system.domain.dto.ExtUploadFileDto;
import com.pulaien.system.service.Open1688Service;
import com.pulaien.system.service.plug.GoodsPlService;
import com.pulaien.web.controller.common.OssController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * Author yuan_lai
 * CreateTime 2024-02-22 9:25
 **/
@RestController
@RequestMapping("/plug/goods" )
@Api(tags = "比价插件配置")
@Slf4j
public class GoodsPlController {

    final
    OssController ossController;


    final GoodsPlService goodsPlService;

    private final Open1688Service open1688Service;

    public GoodsPlController(GoodsPlService goodsPlService, OssController ossController, Open1688Service open1688Service) {
        this.goodsPlService = goodsPlService;
        this.ossController = ossController;
        this.open1688Service = open1688Service;
    }



    @ApiOperation(value = "1688以图搜图上传图图片")
    @PostMapping("/uploadBy1688")
    public AjaxResult uploadBy1688(@Valid ExtUploadFileDto uploadFile) {

        String picUrl = "";
        try{
            AjaxResult ajaxResult = ossController.uploadFile(uploadFile);
            picUrl = (String)ajaxResult.get("url");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        //图片上传
        return goodsPlService.imageUpload(uploadFile , picUrl);

    }


    @ApiOperation(value = "1688以图搜图查询商品列表")
    @GetMapping("/queryGoodsListBy1688Img")
    public AjaxResult queryGoodsListBy1688Img(
            @RequestParam(value = "imageId" ) String imageId,
            ExtGoodsParam extGoodsParam,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {

        return open1688Service.imgQuery(imageId , extGoodsParam , lang);

    }


    @ApiOperation(value = "1688以图搜图")
    @GetMapping("/1688/list")
    public AjaxResult queryGoodsListBy1688(
            @RequestParam(value = "picPath") String picPath,
            ExtGoodsParam extGoodsParam,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) throws IOException {

        if(picPath.contains("_.webp")) {
            picPath = picPath.replace("_.webp", "");
        }
        if(!picPath.contains("http")) {
            picPath = "https:" + picPath;
        }

        //log.info("图片地址 " + picPath);
        return goodsPlService.queryGoodsListBy1688(picPath , extGoodsParam ,lang);

    }




    @ApiOperation(value = "淘宝以图搜图上传图图片")
    @GetMapping("/uploadByTb/path")
    public AjaxResult uploadByTbPath(@RequestParam(value = "picPath") String picPath) {

        if(picPath.contains("_.webp")) {
            picPath = picPath.replace("_.webp", "");
        }
        if(!picPath.contains("http")) {
            picPath = "https:" + picPath;
        }

        try{
            return goodsPlService.uploadByTbPath(picPath);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.success();
        }
    }


    @ApiOperation(value = "淘宝以图搜图上传图地址")
    @PostMapping("/uploadByTb/file")
    public AjaxResult uploadByTbFile(@Valid ExtUploadFileDto uploadFile) {

        String picPath = "";
        try{
            AjaxResult ajaxResult = ossController.uploadFile(uploadFile);
            picPath = (String)ajaxResult.get("url");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        try{
            return goodsPlService.uploadByTbPath(picPath);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.success();
        }


    }




    @GetMapping("/tb/list")
    @ApiOperation(value = "淘宝以图搜图")
    public AjaxResult queryGoodsListByTb(
            @RequestParam(value = "picPath") String picPath,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang,
            ExtGoodsParam extGoodsParam
    ) {
        if(picPath.contains("_.webp")) {
            picPath = picPath.replace("_.webp", "");
        }
        if(!picPath.contains("http")) {
            picPath = "https:" + picPath;
        }
        picPath = picPath + "&cat=5&page=" + extGoodsParam.getPageNum() + "&&lang=" + lang;


        //////
        return goodsPlService.queryGoodsListByTb(picPath , extGoodsParam);
    }






    @ApiOperation(value = "京东以图搜图上传图地址")
    @PostMapping("/uploadByJd/file")
    public AjaxResult uploadByJdFile(@Valid ExtUploadFileDto uploadFile) {

        try{
            return  ossController.uploadFile(uploadFile);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.success();
        }
    }




/*     @ApiOperation(value = "京东以图搜图上传图图片")
    @GetMapping("/uploadByJd/path")
    public AjaxResult uploadByJdPath(@RequestParam(value = "picPath") String picPath) {

        if(picPath.contains(".avif")) {
            picPath = picPath.replace(".avif", "");
        }
        if(!picPath.contains("http")) {
            picPath = "https:" + picPath;
        }

        try{
            return goodsPlService.uploadByJdPath(picPath);
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.success();
        }
    } */


    @GetMapping("/jd/list")
    @ApiOperation(value = "京东以图搜图-查询商品列表")
    public AjaxResult queryGoodsListByJd(
            @RequestParam(value = "picPath") String picPath,
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang,
            ExtGoodsParam extGoodsParam
    ) {
        if(picPath.contains("_.webp")) {
            picPath = picPath.replace("_.webp", "");
        }
        if(!picPath.contains("http")) {
            picPath = "https:" + picPath;
        }
        picPath = picPath + "&cat=5&page=" + extGoodsParam.getPageNum() + "&&lang=" + lang;


        //////
        //return goodsPlService.queryGoodsListByJd(picPath , extGoodsParam);
        return ProductUtil.searchImgInfo(picPath, extGoodsParam.getPageNum(), extGoodsParam.getPageSize(), extGoodsParam.getSortKey(), extGoodsParam.getSort(), extGoodsParam.getLowPrice(), extGoodsParam.getHighPrice(), "jd");
    }




}
